package org.example;

import bean.DB;
import bean.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet("/regServlet")
public class regServlet extends HttpServlet {

    private static final String url = "jdbc:mysql://localhost:3306/student?serverTimezone=GMT";
    private static final String dbUsername = "root";
    private static final String dbPassword = "123456";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        DB db = new DB();
        // 获取用户输入的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String passagain = request.getParameter("passagain");
        String origin = request.getParameter("origin");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String address = request.getParameter("address");
        int user_star = 0;


        try {
            // 加载JDBC驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 创建数据库连接
            try (Connection conn = DriverManager.getConnection(url, dbUsername, dbPassword)) {
                // 插入用户信息的SQL语句
                String sql = "INSERT INTO users (username, password, origin, gender, birthday, address, user_star) VALUES (?, ?, ?, ?, ?, ?, ?)";
                try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                    stmt.setString(1, username);
                    stmt.setString(2, password);
                    stmt.setString(3, origin);
                    stmt.setString(4, gender);
                    stmt.setString(5, birthday);
                    stmt.setString(6, address);
                    stmt.setInt(7, user_star);

                    if (password.equals(passagain)) {
                        if (!loginServlet.validateUser(db, username)) {
                            int result = stmt.executeUpdate();
                            if (result > 0) {
                                ResponseUtils.sendResponse(response, "注册成功", "login.jsp");
                            } else {
                                ResponseUtils.sendResponse(response, "注册失败", "reg.jsp");
                            }
                        } else {
                            ResponseUtils.sendResponse(response, "用户名已存在,注册失败", "reg.jsp");
                        }
                    } else {
                        ResponseUtils.sendResponse(response, "注册失败,两次密码不一致", "reg.jsp");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println("<h3>注册过程中出现错误: " + e.getMessage() + "</h3>");
        }
    }
}
